#! /bin/bash
# rt-upload script for Bartol ori data
# args: mode [ori|1h]
#       station to upload for

LIST="sopo sopb mcmu thul nain pwnk fsmt invk newk"
OPTIONS=""
#OPTIONS="-m1440 --timeout=45"
SLEEP=10
export PYTHONPATH=$HOME/src/python/site-packages

BIN=$PYTHONPATH/nmdb/bin
SUFFIX=`date +%Y%m%d`
MINPARAMS=1

if [ $# -lt "$MINPARAMS" ]; then
  echo "This script needs at least $MINPARAMS command-line arguments (ori or 1h)!"
  exit
fi

if [ "$1" = "ori" ]; then
  EXT="_QL_Minute.txt"
  MODE="ori"
  FEXT="*Min*"
  LOGEXT="1m.log."$SUFFIX
elif [ "$1" = "1h" ]; then
  EXT="_QL_Hour.txt"
  MODE="1h"
  FEXT="*Hour*"
  LOGEXT="1h.log."$SUFFIX
else
  MODE="none"
  echo "Unknown mode: please select ori or 1h"
  exit
fi

# If a second argument is given, use this as station name
if [ -n "$2" ]   # Tested variable is quoted.
then
  LIST=$2
fi

# wait for the data to be available
sleep $SLEEP
echo -n "START:";date
for station in $LIST; do
    STATION="$(echo $station | tr '[:lower:]' '[:upper:]')"
    cp /var/tmp/nmdb/TEMP/$STATION$FEXT /var/tmp/nmdb/
# write log files for each station in $HOME/logs/
    LOG=$HOME/logs/NMDB_$STATION.$LOGEXT
# disable logging:
#    LOG="/dev/null"
    date --utc >>$LOG
    # upload up 120 values (default) with timeout of 30 seconds (default)
    $BIN/upload.py $station $MODE $OPTIONS 2>&1 >> $LOG &
done
# end of script, upload jobs may still be running in the background
echo -n "  END:";date
